/** Description of BrandsBdAdapter.java 
 *
 * @author panther
 * Jayant Pratim Saikia
 * jay.saikia@gmail.com
 *
 */
package com.panther.awp.android.smoker.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

public class BTDbAdapter extends AbstractDbAdapter {

	public BTDbAdapter(Context context) {
		super(context);
	}
	
	@Override
	public long create(ContentValues values) {
		return mDb.insert(BRANDS_TRACK, null, values);
	}
	
	@Override
	public long update(long id, ContentValues values) {
		return mDb.update(BRANDS_TRACK, values, BT_PK_ID+"="+id, null);
	}

	@Override
	public long delete(long id) {
		return mDb.delete(BRANDS_TRACK, BT_PK_ID + "=" + id, null);
	}
	
	@Override
	public Cursor fetchAll() {
		return mDb.query(BRANDS_TRACK, null, null, null, null, null, null);
	}

	@Override
	public Cursor fetch(long id) {
		return mDb.query(BRANDS_TRACK, null, BT_PK_ID+"="+id, null, null, null, null);
	}
	
	@Override
	public Cursor fetchOldest() {
		// TODO Auto-generated method stub
		return null;
	}
	
	@Override
	public Cursor fetchLatest() {
		// TODO Auto-generated method stub
		return null;
	}

	public Cursor fetchBeforeThisBrand(long id) {
		Cursor cursor = mDb.query(true, BRANDS_TRACK, null, BT_PK_ID + " > " + id, null, null, null, BT_PK_ID + " ASC", "1");
		if(cursor.getCount() == 1) {
			return cursor;
		} else {
			cursor.close();
			Cursor cursor2 = mDb.query(true, BRANDS_TRACK, null, BT_PK_ID + " < " + id, null, null, null, BT_PK_ID + " ASC", "1");
			if(cursor2.getCount() == 1) {
				return cursor2;
			} else {
				return mDb.query(BRANDS_TRACK, null, BT_PK_ID+"="+id, null, null, null, null);
			}
		}
	}
	
	public Cursor fetchMostSmokedBrand() {
		return mDb.query(true, BRANDS_TRACK, null, null, null, null, null, BT_NO_OF_CIGGS + " DESC", "1");
	}

}
